//package com.gitee.huxing.course.netty.threadpool;
//
//import io.netty.bootstrap.Bootstrap;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.pool.AbstractChannelPoolMap;
//import io.netty.channel.pool.ChannelPoolMap;
//import io.netty.channel.pool.FixedChannelPool;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import lombok.extern.slf4j.Slf4j;
//
//import java.net.InetSocketAddress;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @Description: 客户端连接池
// * @ProjectName: JavaCourse
// * @Package: com.gitee.huxing.course.netty.threadpool
// * @ClassName: NettyClientPool
// * @Author: huxing
// * @DateTime: 2021-08-22 上午11:54
// */
//@Slf4j
//public class NettyClientPool {
//
//    /** volatile保持线程之间的可见性，连接池的创建是单例，在这里可加可不加 **/
//    volatile private static NettyClientPool nettyClientPool;
//
//    /** key为目标主机的InetSocketAddress对象，value为目标主机对应的连接池 **/
//    public ChannelPoolMap<InetSocketAddress, FixedChannelPool> poolMap;
//
//    final EventLoopGroup group = new NioEventLoopGroup();
//
//    final Bootstrap strap = new Bootstrap();
//
//    private static String addresses = "127.0.0.1:8088";
//
//    volatile private static Map<InetSocketAddress,FixedChannelPool> pools = new HashMap<>(4);
//
//    volatile private static List<InetSocketAddress> addressList;
//
//    private NettyClientPool(){
//        build();
//    }
//
//    /**
//     * @Description: 单例模式创建netty client
//     * @Author: huxing
//     * @param
//     * @return com.gitee.huxing.course.netty.threadpool.NettyClientPool
//     * @Date: 2021/8/22 上午11:59
//     **/
//    public static NettyClientPool getInstance(){
//        if(nettyClientPool == null) {
//            synchronized (NettyClientPool.class) {
//                if(nettyClientPool == null) {
//                    nettyClientPool = new NettyClientPool();
//                }
//            }
//        }
//        return nettyClientPool;
//    }
//
//    /**
//     * @Description:    创建客户端
//     * @Author: huxing
//     * @param
//     * @Date: 2021/8/22 上午11:59
//     **/
//    public void build(){
//        log.info("NettyClientPool build......");
//        strap.group(group).channel(NioSocketChannel.class)
//                .option(ChannelOption.TCP_NODELAY, true)
//                .option(ChannelOption.SO_KEEPALIVE, true);
//        poolMap = new AbstractChannelPoolMap<InetSocketAddress, FixedChannelPool>() {
//            @Override
//            protected FixedChannelPool newPool(InetSocketAddress key) {
//                return new FixedChannelPool(strap.remoteAddress(key),
//                        new NettyChannelPoolHandler(), DataBusConstant.MAX_CONNECTIONS);
//            }
//        };
//        getInetAddresses(addresses);
//
//        for (InetSocketAddress address: addressList){
//            pools.put(address,poolMap.get(address));
//        }
//    }
//}
